package com.xiaoshuidi.cloud.module.finance.controller.admin.voucher;

import cn.hutool.core.collection.CollectionUtil;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.security.core.LoginUser;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.finance.controller.admin.voucher.vo.FinanceVoucherDetailRespVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.voucher.vo.FinanceVoucherInfoRespVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.voucher.vo.FinanceVoucherInfoSearchReqVO;
import com.xiaoshuidi.cloud.module.finance.convert.voucher.FinanceVoucherInfoConvert;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.voucher.FinanceVoucherInfoDO;
import com.xiaoshuidi.cloud.module.finance.service.voucher.FinanceVoucherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

/**
	* @author dujianguang
	*/
@Tag(name = "凭证管理 - 凭证管理")
@RestController
@RequestMapping("/finance/voucher")
@Validated
public class FinanceVoucherController {

		@Resource
		private FinanceVoucherService financeVoucherService;

		@GetMapping("/page")
		@PermitAll
		@Operation(summary = "获取凭证管理")
		public CommonResult<PageResult<FinanceVoucherInfoRespVO>> getInfoPage(@Valid FinanceVoucherInfoSearchReqVO pageVO) {
				PageResult<FinanceVoucherInfoDO> pageResult = financeVoucherService.getInfoPage(pageVO);
				if (CollectionUtil.isEmpty(pageResult.getList())) {
						return success(new PageResult<>(pageResult.getTotal()));
				}
				System.out.println(pageResult.getList().size());
				List<FinanceVoucherInfoRespVO> pageList = new ArrayList<>(pageResult.getList().size());
				pageResult.getList().forEach(c -> {
						FinanceVoucherInfoRespVO financeVoucherInfoRespVO = FinanceVoucherInfoConvert.INSTANCE.convert(c);
						pageList.add(financeVoucherInfoRespVO);
				});
				return success(new PageResult<>(pageList, pageResult.getTotal()));
		}



		@GetMapping("/detail")
		@PermitAll
		@Operation(summary = "获取凭证明细")
		public CommonResult<List<FinanceVoucherDetailRespVO>> getDetail(@RequestParam("voucherId") Long voucherId) {
				return CommonResult.success(financeVoucherService.getDetail(voucherId));
		}



		@GetMapping("/voucher-sync")
		@Operation(summary = "同步凭证")
		public CommonResult<String> voucherSync(@RequestParam("voucherId") Long voucherId) {
				return CommonResult.success(financeVoucherService.syncVoucher(voucherId));
		}
}
